package com.wuneng.ircontrol.myUtil.pic;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import com.wuneng.ircontrol.R;


public class ImageLoaderUtil {
    public static DisplayImageOptions createOptios() {

        return new DisplayImageOptions.Builder().showImageForEmptyUri(R.mipmap.load_faild).showImageOnFail(R.mipmap.load_faild).showImageOnLoading(R.mipmap.loading).cacheInMemory(true).cacheOnDisk(true).imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565)// 防止内存溢出的，图片太多就这这个。还有其他设置
                .build();
    }

    public static DisplayImageOptions createOptios(int defaultResId, boolean isCache) {

        return new DisplayImageOptions.Builder().showImageForEmptyUri(defaultResId).showImageOnFail(defaultResId).showImageOnLoading(defaultResId).cacheInMemory(isCache).cacheOnDisk(isCache).imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565)// 防止内存溢出的，图片太多就这这个。还有其他设置
                .build();
    }

    public static void displayImage(String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios());
    }

    public static void displayImage(String url, ImageView imageView, int defaultResId) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(defaultResId, true));
    }

    /**
     * url 显示图片失败，就用白图显示
     */
    public static void displayWhiteImage(String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.load_faild, true));
    }

    public static void displayFromSDCard(String url, ImageView imageView) {
        url = "file://" + url;
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.photo_bg, true));
    }

    public static void displayFromDrawable(int imageId, ImageView imageView) {
        String url = "drawable://" + imageId;
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.load_faild, true));
    }


    public static void displaySquareImage(String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.load_faild, true));
    }

    /**
     * url 不缓存
     */
    public static void displayWhiteImage(String url, ImageView imageView, boolean isCache) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.load_faild, isCache));
    }

    public static void displayUserImage(String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, createOptios(R.mipmap.load_faild, true));
    }
}
